package com.priox.sigar.server.infrastructure.persistence.naming;

import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.cfg.DefaultNamingStrategy;

import java.util.HashMap;
import java.util.Map;

/**
 * User: aglwkrs
 * Date: 29/11/11
 */
public class SeguridadNamingStrategy extends DefaultNamingStrategy {
    private Map<String, String> standardProperties;

    public SeguridadNamingStrategy() {
        super();
        initializeStandardProperties();
    }

    private void initializeStandardProperties() {
        standardProperties = new HashMap<String, String>();
    }

    /**
     * org.company.domain.SaleTimestamp is converted like this:
     * SaleTimeStamp (unqualify)
     * sale_timestamp (camel case to underscores)
     * sl_tmstmp (remove vowels)
     * t_sl_tmstmp (add T prefix)
     *
     * @param className
     * @return
     */
    public String classToTableName(String className) {
        return tableName(corporatify(StringHelper.unqualify(className)));
    }

    /**
     * This method does nothing additional to an explicitly provided class name.
     * Typically, {@link #propertyToColumnName(String)} is invoked (whenever the
     * column value is not explicitly defined).
     *
     * @param columnName
     * @return
     */
    public String columnName(String columnName) {
        return columnName;
    }

    /**
     * Converts a property like this:
     * org.company.domain.Sale.saleRegion is converted like this:
     * saleRegion (unqualify)
     * sale_region (camel case to underscores)
     * sl_rgn (remove vowels)
     * <p/>
     * Note that some standard properties are mapped automatically to their custom
     * keys.
     *
     * @param propertyName
     * @return
     */
//    public String propertyToColumnName(String propertyName) {
//        return (standardProperties.containsKey(propertyName)) ?
//                standardProperties.get(propertyName) :
//                columnName(corporatify(StringHelper.unqualify(propertyName)));
//    }

    /**
     * Explicitly entered table names are prefixed with the corporate standard
     * "t_". Note that this is only invoked directly if the table name is entered
     * in the mapping file for hibernate (in most cases {@link #classToTableName(String)}
     * will be used).
     *
     * @param tableName
     * @return
     */
    public String tableName(String tableName) {
        if ("DOMAINEVENTENTRY".equals(tableName) || "SNAPSHOTEVENTENTRY".equals(tableName)){
            return  tableName;
        }
        return tableName.substring(0, 4) + "_" + tableName.substring(4, tableName.length());
    }

    /**
     * This method pulls out any of the corporate standard no-nos.
     * <p/>
     * This was adapted from org.hibernate.cfg.ImprovedNamingStrategy.
     *
     * @param name
     * @return
     */
    private String corporatify(String name) {
//        return name.substring(0, 4) + "_" + name.substring(4, name.length());
        return name;
    }

    private boolean isVowel(char aChar) {
        boolean result = false;
        switch (aChar) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                result = true;
                break;
        }
        return result;
    }

}
